package com.wanlian.router.ui.me.message;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.smart.android.ui.app.IntentExtra;
import com.smart.android.ui.tools.BaseListFragment;
import com.smart.android.ui.web.WebActivity;
import com.wanlian.router.R;
import com.wanlian.router.bean.Message;
import com.wanlian.router.bean.MessageCount;
import com.wanlian.router.net.NetWorkingClient;
import com.wanlian.router.net.ResponseData;
import com.wanlian.router.net.internal.INetCallBack;
import com.wanlian.router.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Hyu on 2018/8/31.
 * Email: fvaryu@qq.com
 */
public class MessageFragment extends BaseListFragment {
    @BindView(R.id.image_read)
    ImageView mImageRead;
    @BindView(R.id.image_logo)
    ImageView image_logo;
    @BindView(R.id.tv_title)
    TextView mTitle;
    @BindView(R.id.tv_synopsis)
    TextView mSysnopsis;

    @BindView(R.id.tv_time)
    TextView mTime;
    Message mMessage = new Message();
    private MessageAdapter mMessageAdapter;
    private List<Message> mMessages;

    public static MessageFragment newInstance() {
        return new MessageFragment();
    }


    @Override
    protected int layout() {
        return R.layout.fragment_message;
    }

    @Override
    protected void initUI(View view) {
        super.initUI(view);

        getEmptyLayout().setImageTipEmptyResId(R.drawable.image_empty_message);
        setEmptyText("还没有消息哦~");

        mMessages = new ArrayList<>();

        mMessage.setId(-1);
        mMessage.setHasLook(true);
//        mMessage.setTitle("系统公告");

        setAdapter(mMessageAdapter = new MessageAdapter(getActivity(), mMessages));

        getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Message message = (Message) parent.getAdapter().getItem(position);


                if (!message.isHasLook()) {
                    message.setHasLook(true);
                    mMessageAdapter.notifyDataSetChanged();
                    NetWorkingClient.getInstance().updateMessageRead(getActivity(), message.getId(), new INetCallBack() {
                        @Override
                        public void onFinish(ResponseData response, @Nullable Object o) {

                        }
                    });
                }

                if (TextUtils.isEmpty(message.getUrl())) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable(IntentExtra.EXTRA_OBJ, message);

                    redirectActivity(MessageDetailActivity.class, bundle);
                } else {
                    WebActivity.fire(getActivity(), message.getUrl());
                }

            }
        });
    }

    @Override
    protected void initData(View view) {
        super.initData(view);

    }

    @OnClick(R.id.rl_sys)
    void sys() {
        redirectActivity(SystemNoticeActivity.class);
    }

    public void sys(Message message) {
        image_logo.setImageResource(R.drawable.drawable_sys_message);
        if (message.getNoticeTime() > 0) {
            mTime.setText(Utils.getDate(message.getNoticeTime()));
        } else {
            mTime.setText("");
        }


//        mTitle.setText(message.getTitle());
        if (TextUtils.isEmpty(message.getContent())) {
            mSysnopsis.setText("当前没有系统公告呢");
        }else {
            mSysnopsis.setText(message.getContent());

        }
        if (message.isHasLook()) {
            mImageRead.setVisibility(View.GONE);
        } else {
            mImageRead.setVisibility(View.VISIBLE);
        }
    }

    private void querySys() {
        NetWorkingClient.getInstance().querySysNotice(getActivity(), new INetCallBack<Message>() {
            @Override
            public void onFinish(ResponseData response, @Nullable Message message) {
                if (response.isSuccess()) {
                    if (message != null) {
                        Message m = mMessage;
//                        m.setTitle("系统公告");
                        m.setContent(message.getTitle());
                        m.setNoticeTime(message.getNoticeTime());

                        sys(m);

                        querySysCount();
                    } else {
                        mMessage.setContent("当前没有系统公告呢");
                    }
                } else {
                    mMessage.setContent("当前没有系统公告呢");
                }
            }
        });
    }

    private void querySysCount() {

        NetWorkingClient.getInstance().querySysMessageCount(getActivity(), new INetCallBack<MessageCount>() {
            @Override
            public void onFinish(ResponseData response, @Nullable MessageCount messageCount) {
                if (response.isSuccess()) {
                    if (mMessage.getId() == -1) {
                        mMessage.setHasLook(messageCount == null || messageCount.getCount() == 0);
                        sys(mMessage);
                    }
                }
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        querySysCount();
    }

    @Override
    protected void loadData(final boolean isRefresh) {
        super.loadData(isRefresh);

        querySys();

        if (!canToLoad(isRefresh)) {
            return;
        }

        NetWorkingClient.getInstance().queryMessage(getActivity(), getPageInfo(), new INetCallBack<List<Message>>() {
            @Override
            public void onFinish(ResponseData response, @Nullable List<Message> messages) {
                resetPull();
                if (response.isSuccess()) {
                    if (isRefresh) {
                        mMessages.clear();
                    }

                    if (messages != null) {
                        mMessages.addAll(messages);
                    }
                    mMessageAdapter.notifyDataSetChanged();
                }

                if (mMessages.isEmpty()) {
                    showEmpty();
                }

            }
        });
    }
}
